import copy

class Param:
    def __init__(self):
        self.name = None
        self.required = False
        self.multi = False
        self.negative = False
        self.candidates = []
        self.result = []
    
    def make(self):
        self.candidates.sort()
        if not self.required:
            self.result.append('')
        if not self.negative:
            self.candidates = filter(lambda x:not x.startswith('-'), self.candidates)
        if not self.multi:
            self.result = self.candidates
        else:
            for i in range(1, len(self.candidates)+1):
                path = []
                ret = []
                def combi(begin):
                    if len(path)==i: 
                        ret.append(copy.copy(path))
                        return
                    for i in range(begin, len(self.candidates)):
                        path.append(self.candidates[i])
                        combi(begin+1)
                combi(0)
                self.result.extend([','.join(r) for r in ret])
        

N = int(raw_input())
params = []
for _ in range(N):
    param = Param()
    name, r, m, n = map(int, raw_input().split(' '))
    param.name = name
    param.required = r==1
    param.multi = m==1
    param.negative = n==1
    param.candidates = raw_input().split(',')
    param.make()
    params.append(param)
result = []
acc = []
def traverse(i):
    if i==len(params):
        result.append(copy.copy(acc))